﻿@model Role
@{
    
    DBEntities db = new DBEntities();
    ViewBag.Title = "Index";
    var parentNode = new TreeNode();
    parentNode.ID = "";
    parentNode.Name = "全部";
    parentNode.Open = true;
    parentNode.Children = new Service.MenuService(db).TreeNode(null, Model == null ? new List<string>() : Model.Menus.Select(p => p.ID));
    parentNode.Checked = parentNode.Children.Any(p => p.Checked);
}
<script type="text/javascript">
function getCheckedValues()
{
   var checkedTreeIDs = "";
    var zTree = $.fn.zTree.getZTreeObj("RoleMenusTree");
    var nodes = zTree.getCheckedNodes(true);
    
    if(null != nodes && nodes.length > 0){
        for (var i=0; i<nodes.length; i++) {
            if (nodes[i].Value!=null&&nodes[i].Value!="") {
                checkedTreeIDs += nodes[i].Value + ",";
            }
             
	    }
    }
    if(checkedTreeIDs.length > 0){
        checkedTreeIDs = checkedTreeIDs.substring(0,checkedTreeIDs.length-1);
    }
    $("#RoleMenus").val(checkedTreeIDs);
}
var zNodes = @(Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(parentNode)));
var setting = {
    check: {enable: true},
    view: {
		selectedMulti: false,
        showIcon: true
    },
    data: {
        key: {
            id: "ID",
			name: "name",
			children: "children",
			title: "name"
		}
    },
    callback:{
    onCheck: getCheckedValues
    }
};
    $.fn.zTree.init($("#RoleMenusTree"), setting, zNodes);
</script>
<textarea id="RoleMenus" name="RoleMenus" style="display: none;">@string.Join(",", Model == null ? new List<string>() : Model.Menus.Select(p => p.ID))</textarea>
<table class="default">
    <thead>
        <tr>
            <th>
                菜单访问权限
            </th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <ul id="RoleMenusTree" class="ztree">
                </ul>
            </td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td align="center">@Html.Partial("_Button_Submit")
            </td>
        </tr>
    </tfoot>
</table>
